//
//  htvmylogin.m
//  MaiYaApp
//
//  Created by zhangwq on 15/11/20.
//  Copyright © 2015年 HT. All rights reserved.
//

#import "htvmylogin.h"
#import "HTFrameKit.h"
#import "HTSysKit.h"
#import "HTFrameConvert.h"


@interface htvmylogin (){
    CGFloat width;
    CGFloat height;
    UITextField *txtUserid;
    UITextField *txtPassword;
    UIWebView *webView;
    UIColor *backcolor;
}

@end

@implementation htvmylogin

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    
    CGRect rect = [[UIScreen mainScreen] bounds];
    CGSize size = rect.size;
    width = size.width;
    height = size.height;
    
    txtUserid=[[UITextField alloc]init];
    txtPassword=[[UITextField alloc]init];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(moveKeyAction:)];
    [self.view addGestureRecognizer:tapGesture];
    
    backcolor=[UIColor colorWithRed:150.0/255 green:150.0/255 blue:150.0/255 alpha:1.0f];
    
    self.navigationController.navigationBar.tintColor=[UIColor blackColor];
    [self.navigationController.navigationBar setTitleTextAttributes:
     
     @{NSFontAttributeName:[UIFont systemFontOfSize:16],
       
       NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    [self createControl];

    
    

    
}

-(void)initData{
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:@"" forKey:@"cityCode"];
    [defaults setObject:@"" forKey:@"cityName"];
    [defaults setObject:@"" forKey:@"userID"];
    [defaults setObject:@"" forKey:@"mobile"];
    [defaults setObject:@"" forKey:@"upFlag"];

    
    //设置平台服务地址
    [HTFrameNet defaultServerUrlSet:@"http://www.wazhiyuan.com:8080"];//121.40.52.190:88
    
    NSDictionary *dict=@{ @"erpserver":@"http://www.wazhiyuan.com:8080",@"erpvirdir":@"wzy",@"cityCode":@"",@"city":@"",@"userID":@"",@"mobile":@"",@"upFlag":@"0"};
    
    [HTSysParam loadParamWithDefault:dict];
    
    
}

-(void)createControl{
    
    
    UIView *uv_user=[[UIView alloc]initWithFrame:CGRectMake(0, 90, width, 50)];
    uv_user.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:uv_user];
    
    UIImageView *line=[[UIImageView alloc]initWithFrame:CGRectMake(0, 139, width, 0.6)];
    line.image=[UIImage imageNamed:@"line2"];
    [self.view addSubview:line];
    
    UIView *uv_password=[[UIView alloc]initWithFrame:CGRectMake(0, 140, width, 50)];
    uv_password.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:uv_password];
    

    
    UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(30, 12.5, 25, 25)];
    iv.image=[UIImage imageNamed:@"user"];
    [uv_user addSubview:iv];
    
    UIImageView *iv2=[[UIImageView alloc]initWithFrame:CGRectMake(30, 12.5, 25, 25)];
    iv2.image=[UIImage imageNamed:@"password"];
    [uv_password addSubview:iv2];
    
    
    txtUserid=[[UITextField alloc]initWithFrame:CGRectMake(85, 10, width-120, 30)];
    txtUserid.backgroundColor=[UIColor whiteColor];
    txtUserid.tintColor=backcolor;
    txtUserid.placeholder=@"请输入账号";
    txtUserid.font=[UIFont fontWithName:@"Arial" size:14.0];
    [uv_user addSubview:txtUserid];
    
    txtPassword=[[UITextField alloc]initWithFrame:CGRectMake(85, 10, width-120, 30)];
    txtPassword.tintColor=backcolor;
    txtPassword.backgroundColor=[UIColor whiteColor];
    txtPassword.placeholder=@"请输入密码";
    txtPassword.font=[UIFont fontWithName:@"Arial" size:14.0];
    txtPassword.secureTextEntry = YES;
    [uv_password addSubview:txtPassword];
    
    
    UIButton *btnLogin=[[UIButton alloc]initWithFrame:CGRectMake(30, 210, width-60, 45)];
    [btnLogin setTitle:@"登录" forState:UIControlStateNormal];
    [btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnLogin.font=[UIFont fontWithName:@"Arial" size:16.0];
    btnLogin.backgroundColor=[UIColor colorWithRed:194.0/255 green:7.0/255 blue:30.0/255 alpha:1.0f];
    btnLogin.layer.cornerRadius=5.0;
    [btnLogin addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin];
    
    
}




-(void)moveKeyAction:(UITapGestureRecognizer *)sender {
    [txtUserid resignFirstResponder];
    [txtPassword resignFirstResponder];
}

-(void)loginAction:(id)sender{
    if([txtUserid.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入账号"];
        [txtUserid becomeFirstResponder];
        return;
    }
    if([txtPassword.text isEqualToString:@""]){
        [HTFrameAlert hmAlertInfo:@"请输入密码"];
        [txtPassword becomeFirstResponder];
        return;
    }
    
    
    NSString *serverUrl=[[NSString alloc]initWithFormat:@"%@\/%@\/",[HTSysParam erpServer],[HTSysParam erpVirDir]];
    HTFrameNet *htnet=[[HTFrameNet alloc] initWithServerUrl:serverUrl];
    
    
    NSDictionary *dict =@{@"mobile":txtUserid.text,@"password":txtPassword.text};
    [htnet hmNetWCFmethodName:@"web/member/signin" andDic:dict complete:^(NSString *backstr)
     {
         NSDictionary *formdict=[HTFrameNet hmConvertDictFromJsonStr:backstr];//表单单头

         int status=[[formdict objectForKey:@"status"]intValue];
         if(status==0){
             
             NSDictionary *body=[formdict objectForKey:@"body"];
             NSDictionary *member=[body objectForKey:@"DataMember"];
             

             NSString *userID=[member objectForKey:@"id"];
             NSString *mobile=[member objectForKey:@"mobile"];
             
             
             NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
             [defaults setObject:@"" forKey:@"cityCode"];
             [defaults setObject:@"" forKey:@"cityName"];
             [defaults setObject:userID forKey:@"userID"];
             [defaults setObject:@"" forKey:@"clubID"];
             [defaults setObject:mobile forKey:@"mobile"];
             [defaults setObject:@"0" forKey:@"upFlag"];
             
             //设置本地数据库名
             [HTFrameSysUtils dbNameSet:@"htmandb"];
             //加载参数
             [HTSysParam loadParamFormLocalDB];
             
                          
             //设置平台服务地址
             [HTFrameNet defaultServerUrlSet:@"http://www.wazhiyuan.com:8080"];//121.40.136.216:88
             
             NSDictionary *dict=@{ @"erpserver":@"http://www.wazhiyuan.com:8080",@"erpvirdir":@"wzy",@"userID":userID,@"mobile":mobile,@"upFlag":@"0"};
             
             [HTSysParam loadParamWithDefault:dict];
             
             NSString *storyName=@"";
             if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
                 storyName=@"HTMain_New";
             }
             else{
                 storyName=@"HTMain_IPAD";
             }
             
             //找到当前的storyboard对象
             UIStoryboard *story = [UIStoryboard storyboardWithName:storyName bundle:nil];
             UIViewController *main = [story instantiateViewControllerWithIdentifier:@"mdiVC"];
             [self presentModalViewController:main animated:YES];
             
         }
         else{
             NSDictionary *error=[formdict objectForKey:@"error"];
             NSString *code=[error objectForKey:@"code"];
             if([code isEqualToString:@"110101"]){
                 [HTFrameAlert hmAlertInfo:@"账号不存在"];
             }
             if([code isEqualToString:@"120101"]){
                 [HTFrameAlert hmAlertInfo:@"密码错误"];
             }
             [webView removeFromSuperview];
             
             
         }
         
         
     }];
    

    
}



-(void)viewDidAppear:(BOOL)animated
{
    if(!animated)
    {
        if(![[HTSysParam telNum] isEqualToString:@""]){
            txtUserid.text=[HTSysParam telNum];
            [txtPassword becomeFirstResponder];
        }
    }
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



- (IBAction)backAction:(UIButton *)sender {
    //[self dismissViewControllerAnimated: YES completion: nil ];
    NSString *storyName=@"";
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        storyName=@"HTMain_New";
    }
    else{
        storyName=@"HTMain_IPAD";
    }
    
    //找到当前的storyboard对象
    UIStoryboard *story = [UIStoryboard storyboardWithName:storyName bundle:nil];
    UIViewController *main = [story instantiateViewControllerWithIdentifier:@"mdiVC"];
    [self presentModalViewController:main animated:YES];
}

- (IBAction)registerAction:(UIButton *)sender {
    UITapGestureRecognizer *sub=(UITapGestureRecognizer *)sender;
    NSString *storyboard=@"MYRegister";
    NSString *Identifier=@"MYRegister";
    UIStoryboard *valueView = [UIStoryboard storyboardWithName:storyboard bundle:nil];
    UIViewController *uv=[valueView instantiateViewControllerWithIdentifier:Identifier];
    UINavigationController *baNavigation = [[UINavigationController alloc] initWithRootViewController:uv];
    [self presentModalViewController:baNavigation animated:YES];
    
    //返回
    
    NSLog(@"1111");
}
@end
